// 模拟从服务器获取的文件列表
const audioFiles = [
  'unit1_lesson1.mp3', 'unit1_lesson2.mp3', 'unit1_lesson3.mp3', 'unit1_lesson4.mp3', 'unit1_review.mp3', 'unit2_lesson1.mp3', 'unit2_lesson2.mp3', 'unit2_lesson3.mp3', 'unit2_lesson4.mp3', 'unit2_review.mp3'
];


// 模拟英文文本和翻译 ,,
const textData = {
  'unit1_lesson1.mp3': {
    english: 'Hello, how are you?',
    chinese: '你好，你怎么样？'
  },
  'unit1_lesson2.mp3': {
    english: 'I am fine, thank you.',
    chinese: '我很好，谢谢你。'
  },
  'unit2_lesson1.mp3': {
    english: 'What\'s your name?',
    chinese: '你叫什么名字？'
  },
  'unit2_lesson2.mp3': {
    english: 'My name is Tom.',
    chinese: '我叫汤姆。'
  }
};

// 解析文件名，区分单元和课节
const units = {};
audioFiles.forEach(file => {
  const [unit, lesson] = file.split('_');
  if (!units[unit]) {
    units[unit] = {
      unitName: unit,
      lessons: []
    };
  }
  units[unit].lessons.push({
    name: lesson.replace('.mp3', ''),
    src: file
  });
});

const unitList = document.getElementById('unitList');
const audioPlayer = document.getElementById('audioPlayer');
const currentPlaying = document.getElementById('currentPlaying');
const textDisplay = document.getElementById('textDisplay');
const slowPlay1 = document.getElementById('slowPlay1');
const slowPlay2 = document.getElementById('slowPlay2');
const repeatPlay = document.getElementById('repeatPlay');
const pausePlay = document.getElementById('pausePlay');

// 动态生成单元和课程列表
Object.values(units).forEach(unit => {
  const unitDiv = document.createElement('div');
  unitDiv.classList.add('unit');

  const unitTitle = document.createElement('h2');
  unitTitle.textContent = unit.unitName;
  unitDiv.appendChild(unitTitle);

  const lessonList = document.createElement('ul');
  lessonList.classList.add('lesson-list');

  unit.lessons.forEach(lesson => {
    const listItem = document.createElement('li');
    listItem.textContent = lesson.name;
    listItem.addEventListener('click', () => {
      audioPlayer.src = lesson.src;
      audioPlayer.play();
      currentPlaying.textContent = `正在播放：${unit.unitName} - ${lesson.name}`;
      const text = textData[lesson.src];
      if (text) {
        textDisplay.textContent = `英文：${text.english}  中文：${text.chinese}`;
      } else {
        textDisplay.textContent = '';
      }
    });
    lessonList.appendChild(listItem);
  });

  unitDiv.appendChild(lessonList);
  unitList.appendChild(unitDiv);
});

// 慢放一级
slowPlay1.addEventListener('click', () => {
  audioPlayer.playbackRate = 0.75;
});

// 慢放二级
slowPlay2.addEventListener('click', () => {
  audioPlayer.playbackRate = 0.5;
});

// 重复播放
repeatPlay.addEventListener('click', () => {
  audioPlayer.loop = !audioPlayer.loop;
  repeatPlay.textContent = audioPlayer.loop ? '取消重复播放' : '重复播放';
});

// 暂停
pausePlay.addEventListener('click', () => {
  if (audioPlayer.paused) {
    audioPlayer.play();
    pausePlay.textContent = '暂停';
  } else {
    audioPlayer.pause();
    pausePlay.textContent = '继续播放';
  }
});